package com.itheima.appserver;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.itheima.autoconfig.template.OssTemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServerApplication.class)
public class test {
    @Autowired
    private OssTemplate ossTemplate;

    @Test
    public void testup() throws FileNotFoundException {
        String path = "C:\\Users\\16023\\Desktop\\2.jpg";
        FileInputStream fileInputStream = new FileInputStream(new File(path));
        String imageUrl = ossTemplate.upload(path, fileInputStream);
        System.out.println(imageUrl);
    }

    @Test
    public void testoss() throws FileNotFoundException {
        //1.配置图片路径
        String path = "C:\\Users\\16023\\Desktop\\OIP-C.jpg";
        //2.构造输入流
        FileInputStream fileInputStream = new FileInputStream(new File(path));
        //3.拼写图片路径
        String filename = new SimpleDateFormat("yyyy/MM/dd").format(new Date())
                + "/" + UUID.randomUUID().toString() + path.substring(path.lastIndexOf("."));
        String endpoint = "oss-cn-shenzhen.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tMQPQ4WBfhEDHnLjNU7";
        String accessKeySecret = "oC48RogE19X9rjWbUIOa818VN9dl8X";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 创建PutObject请求。
        ossClient.putObject("tanhuawsj", filename, fileInputStream);

        ossClient.shutdown();
    }
}
